
libname AZ_ED "D:\Data\Emergency Department\ED Arizona (2005-2009)";
libname CA_ED "D:\Data\Emergency Department\ED California (2005-2008)";
libname IA_ED "D:\Data\Emergency Department\ED Iowa (2004-2009)";
libname NJ_ED "D:\Data\Emergency Department\ED New Jersey (2004-2009)";
libname WI_ED "D:\Data\Emergency Department\ED Wisconsin (2004-2009)";

libname AZ_INP "D:\Data\Hospital Inpatient Records\Hospital Arizona (1990-2009)";
libname CA_INP "D:\Data\Hospital Inpatient Records\Hospital California HCUP (2004-2008)";
libname IA_INP "D:\Data\Hospital Inpatient Records\Hospital Iowa (2004-2009)";
libname NJ_INP "D:\Data\Hospital Inpatient Records\Hospital New Jersey (1990-2009)";
libname WI_INP "D:\Data\Hospital Inpatient Records\Hospital Wisconsin (2004-2009)";

libname AHA "C:\Data\AHA Survey";

%let keep_v1 = bmonth byear amonth ayear ECODE1 DX1 FEMALE DISPUNIFORM PAY1 DSHOSPID;


%macro pull_s(lib,file1,file2,file3,keep_v2,keep_v3);
data junk1 (drop = l_amonth l_DSHOSPID l2_DSHOSPID l2_amonth);
   format state $2.;
   set &lib..&file1. (keep = &keep_v1. &keep_v2. &keep_v3. );
   file = "&file1.";
   state = substr("&lib.",1,2);
   if substr("&lib.",4,2) = 'IN' then inpatient = 1; else inpatient = 0; 
   l_amonth = lag(amonth);
   l_DSHOSPID = lag(DSHOSPID);
   l2_amonth = lag2(amonth);
   l2_DSHOSPID = lag2(DSHOSPID);
   if inpatient = 0 and female = . and amonth = . and state = 'CA' and 
      l_amonth ne . and l_DSHOSPID = DSHOSPID then amonth = l_amonth; 
   if inpatient = 0 and female = . and amonth = . and state = 'CA' and 
      l_amonth = . and l2_DSHOSPID = DSHOSPID then amonth = l2_amonth; 
   months_23 = intck('month',mdy(bmonth,14,byear+23),mdy(amonth,15,ayear));
   if months_23 = . then bad_date = 1; else bad_date = 0;
   if inpatient = 1 and (&keep_v2. = "1" or &keep_v3. = "7") then from_ED = 1; else from_ED = 0;
   if inpatient = 0 and DISPUNIFORM = 2 then trans_to_hosp = 1; else trans_to_hosp = 0;  
   if DISPUNIFORM = 20 then died = 1; else died = 0;
   if substr(DX1,1,2) in ('63','64','65','66','67') then preg_related = 1; else preg_related = 0;
   if pay1 = 2 then medicaid = 1; else medicaid = 0;
   if pay1 = 3 then private = 1; else private = 0;
   if pay1 = 4 then self_pay = 1; else self_pay = 0;
   if pay1 not in (2,3,4) then other_ins = 1; else other_ins = 0;
   if (months_23 >= -80 and months_23 <= 80) or bad_date = 1;
run;
proc sort data = junk1; by DSHOSPID; run;
proc sort data = &lib..&file2. (keep = DSHOSPID AHAID) out = junk2;  by AHAID; run;
data junk3;
   merge junk2 (in = ina) AHA.&file3.;
   by AHAID;
   if ina = 1; 
run;

proc sort data = junk3;   by DSHOSPID; run;

data &file1. (drop = DSHOSPID);
   merge junk1 (in = ina) junk3;
   by DSHOSPID; 
   if ina = 1;
run;

proc datasets;
   delete junk1 junk2 junk3;
run;
%mend;

%pull_s(AZ_ED,az_seddc_2005_core,Az_seddc_2005_ahal,Aha_extract2005,byear,byear); 
%pull_s(AZ_ED,az_seddc_2006_core,Az_seddc_2006_ahal,Aha_extract2006,byear,byear);
%pull_s(AZ_ED,az_seddc_2007_core,Az_seddc_2007_ahal,Aha_extract2007,byear,byear);
%pull_s(AZ_ED,az_seddc_2008_core,Az_seddc_2008_ahal,Aha_extract2008,byear,byear);  
%pull_s(AZ_ED,az_seddc_2009_core,Az_seddc_2009_ahal,Aha_extract2009,byear,byear);  

%pull_s(CA_ED,ca_seddc_2005_core,ca_seddc_2005_ahal,Aha_extract2005,byear,byear);
%pull_s(CA_ED,ca_seddc_2006_core,ca_seddc_2006_ahal,Aha_extract2006,byear,byear);
%pull_s(CA_ED,ca_seddc_2007_core,ca_seddc_2007_ahal,Aha_extract2007,byear,byear);


%pull_s(IA_ED,ia_seddc_2004_core,ia_seddc_2004_ahal,Aha_extract2004,byear,byear);
%pull_s(IA_ED,ia_seddc_2005_core,ia_seddc_2005_ahal,Aha_extract2005,byear,byear);
%pull_s(IA_ED,ia_seddc_2006_core,ia_seddc_2006_ahal,Aha_extract2006,byear,byear);
%pull_s(IA_ED,ia_seddc_2007_core,ia_seddc_2007_ahal,Aha_extract2007,byear,byear);
%pull_s(IA_ED,ia_seddc_2008_core,ia_seddc_2008_ahal,Aha_extract2008,byear,byear);  
%pull_s(IA_ED,ia_seddc_2009_core,ia_seddc_2009_ahal,Aha_extract2009,byear,byear);  

%pull_s(NJ_ED,nj_seddc_2004_core,nj_seddc_2004_ahal,Aha_extract2004,byear,byear);
%pull_s(NJ_ED,nj_seddc_2005_core,nj_seddc_2005_ahal,Aha_extract2005,byear,byear);
%pull_s(NJ_ED,nj_seddc_2006_core,nj_seddc_2006_ahal,Aha_extract2006,byear,byear);
%pull_s(NJ_ED,nj_seddc_2007_core,nj_seddc_2007_ahal,Aha_extract2007,byear,byear);
%pull_s(NJ_ED,nj_seddc_2008_core,nj_seddc_2008_ahal,Aha_extract2008,byear,byear);  
%pull_s(NJ_ED,nj_seddc_2009_core,nj_seddc_2009_ahal,Aha_extract2009,byear,byear);  

%pull_s(WI_ED,wi_seddc_2004_core,wi_seddc_2004_ahal,Aha_extract2004,byear,byear);
%pull_s(WI_ED,wi_seddc_2005_core,wi_seddc_2005_ahal,Aha_extract2005,byear,byear);
%pull_s(WI_ED,wi_seddc_2006_core,wi_seddc_2006_ahal,Aha_extract2006,byear,byear);
%pull_s(WI_ED,wi_seddc_2007_core,wi_seddc_2007_ahal,Aha_extract2007,byear,byear);
%pull_s(WI_ED,wi_seddc_2008_core,wi_seddc_2008_ahal,Aha_extract2008,byear,byear);  
%pull_s(WI_ED,wi_seddc_2009_core,wi_seddc_2009_ahal,Aha_extract2009,byear,byear);  



%pull_s(AZ_INP,az_sidc_2005_core,Az_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(AZ_INP,az_sidc_2006_core,Az_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(AZ_INP,az_sidc_2007_core,Az_sidc_2007_ahal,Aha_extract2007,asource,PointOfOriginUB04);
%pull_s(AZ_INP,az_sidc_2008_core,Az_sidc_2008_ahal,Aha_extract2008,byear,PointOfOriginUB04);  
%pull_s(AZ_INP,az_sidc_2009_core,Az_sidc_2009_ahal,Aha_extract2009,byear,PointOfOriginUB04);  

%pull_s(CA_INP,ca_sidc_2005_core,ca_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(CA_INP,ca_sidc_2006_core,ca_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(CA_INP,ca_sidc_2007_core,ca_sidc_2007_ahal,Aha_extract2007,asource,byear);


%pull_s(IA_INP,ia_sidc_2004_core,ia_sidc_2004_ahal,Aha_extract2004,asource,byear);
%pull_s(IA_INP,ia_sidc_2005_core,ia_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(IA_INP,ia_sidc_2006_core,ia_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(IA_INP,ia_sidc_2007_core,ia_sidc_2007_ahal,Aha_extract2007,asource,PointOfOriginUB04);
%pull_s(IA_INP,ia_sidc_2008_core,ia_sidc_2008_ahal,Aha_extract2008,byear,PointOfOriginUB04);  
%pull_s(IA_INP,ia_sidc_2009_core,ia_sidc_2009_ahal,Aha_extract2009,byear,PointOfOriginUB04);  


%pull_s(WI_INP,wi_sidc_2004_core,wi_sidc_2004_ahal,Aha_extract2004,asource,byear);
%pull_s(WI_INP,wi_sidc_2005_core,wi_sidc_2005_ahal,Aha_extract2005,asource,byear);
%pull_s(WI_INP,wi_sidc_2006_core,wi_sidc_2006_ahal,Aha_extract2006,asource,byear);
%pull_s(WI_INP,wi_sidc_2007_core,wi_sidc_2007_ahal,Aha_extract2007,asource,byear);
%pull_s(WI_INP,wi_sidc_2008_core,wi_sidc_2008_ahal,Aha_extract2008,byear,PointOfOriginUB04);  
%pull_s(WI_INP,wi_sidc_2009_core,wi_sidc_2009_ahal,Aha_extract2009,byear,PointOfOriginUB04);   



*Combine the files;
data inter.rep_insurance23 (drop = from_ED trans_to_hosp bmonth byear amonth ayear bad_date asource PointOfOriginUB04);
   set                    az_seddc_2005_core az_seddc_2006_core az_seddc_2007_core az_seddc_2008_core az_seddc_2009_core 
                          ca_seddc_2005_core ca_seddc_2006_core ca_seddc_2007_core
       ia_seddc_2004_core ia_seddc_2005_core ia_seddc_2006_core ia_seddc_2007_core ia_seddc_2008_core ia_seddc_2009_core 
       nj_seddc_2004_core nj_seddc_2005_core nj_seddc_2006_core nj_seddc_2007_core nj_seddc_2008_core nj_seddc_2009_core
       wi_seddc_2004_core wi_seddc_2005_core wi_seddc_2006_core wi_seddc_2007_core wi_seddc_2008_core wi_seddc_2009_core
                          az_sidc_2005_core  az_sidc_2006_core  az_sidc_2007_core  az_sidc_2008_core  az_sidc_2009_core 
                          ca_sidc_2005_core  ca_sidc_2006_core  ca_sidc_2007_core
       ia_sidc_2004_core  ia_sidc_2005_core  ia_sidc_2006_core  ia_sidc_2007_core  ia_sidc_2008_core  ia_sidc_2009_core 
       wi_sidc_2004_core  wi_sidc_2005_core  wi_sidc_2006_core  wi_sidc_2007_core  wi_sidc_2008_core  wi_sidc_2009_core;
	if bad_date = 1 then delete;
	if trans_to_hosp = 1 then delete;
    if inpatient = 1 and from_ED = 0 then delete; 
    ad_date = mdy(amonth,15,ayear);
	if state in ('AZ','CA') and ad_date < mdy(1,1,2005) then delete;
	if state in ('IA','WI') and ad_date < mdy(1,1,2004) then delete;
	visit = 1;
   if cntrl in (12,13,14,15,16) then cntrl_public = 1; else cntrl_public = 0;
   if cntrl in (21,23) then cntrl_non_profit = 1; else cntrl_non_profit = 0;
   if cntrl in (31,32,33) then cntrl_for_profit = 1; else cntrl_for_profit = 0;
   if cntrl not in (12,13,14,15,16,21,23,31,32,33) then cntrl_other = 1; else cntrl_other = 0;
   if female = 1 then male = 0;
   if female = 0 then male = 1;
run;



